﻿#pragma once
#include<iostream>
#include<assert.h>

using namespace std;

template<class K,class V>
struct AVLTreeNode
{
	pair<K, V> _kv;
	struct AVLTreeNode<K, V>* _left;
	struct AVLTreeNode<K, V>* _right;
	struct AVLTreeNode<K, V>* _parent;
	int _bf;

	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}
};

template<class K,class V>
class AVLTree
{
	typedef AVLTreeNode<K,V> Node;
public:
	bool Insert(const pair<K,V>& kv)
	{
		//插入节点
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while(cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if(kv.first > cur->_kv.first )
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		if (kv.first < parent->_kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		//更新平衡因子

		while (parent)
		{
			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}

			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				//不为零继续更新
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				// 旋转
				if (parent->_bf == -2 && cur->_bf == -1)
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
				}
				else
				{
					assert(false);
				}
				break;
			}
			else 
			{
				//出现平衡因子为二的就跳出了不可能有3
				//这种情况说明出错了
				assert(false);
			}
		}

		return true;
	}
	void RotateR(Node* parent)
	{
		Node* SubLNode = parent->_left;
		Node* SubLRNode = SubLNode->_right;

		parent->_left = SubLRNode;
		SubLNode->_right = parent;

		//定义PPNode保存parent的父亲节点，避免其被后续操作覆盖导致avl断开
		Node* PPNode = parent->_parent;

		parent->_parent = SubLNode;
		//防止parent为头节点，导致该操作造成空指针的解引用
		if (SubLRNode)
			SubLRNode->_parent = parent;

		if (PPNode==nullptr)
		{
			_root = SubLNode;
			SubLNode->_parent = nullptr;
		}
		else
		{
			if (PPNode->_left == parent)
			{
				PPNode->_left = SubLNode;
			}
			else
			{
				PPNode->_right = SubLNode;
			}
			SubLNode->_parent = PPNode;
		}

		//最后不要忘记更新对应节点的平衡因子
		parent->_bf = SubLNode->_bf = 0;
	}
	void RotateL(Node* parent)
	{
		Node* SubRNode = parent->_right;
		Node* SubRLNode = SubRNode->_left;
		Node* PPNode = parent->_parent;

		SubRNode->_left = parent;
		parent->_parent = SubRNode;

		parent->_right = SubRLNode;
		if (SubRLNode)
			SubRLNode->_parent = parent;

		if (PPNode==nullptr)
		{
			_root = SubRNode;
			SubRNode->_parent = nullptr;
		}
		else
		{
			if (PPNode->_left == parent)
				PPNode->_left = SubRNode;
			else
				PPNode->_right = SubRNode;
			SubRNode->_parent = PPNode;
		}
		parent->_bf = SubRNode->_bf = 0;
	}
	void RotateLR(Node* parent)
	{
		Node* SubLNode = parent->_left;
		Node* SubLRNode = SubLNode->_right;
		int bf = SubLRNode->_bf;

		RotateL(SubLNode);
		RotateR(parent);

		if (bf == 0)
		{
			SubLNode->_bf = 0;
			SubLRNode->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			SubLNode->_bf = -1;
			SubLRNode->_bf = 0;
			parent->_bf = 0;
		}
		else if( bf == -1 )
		{
			SubLNode->_bf = 0;
			SubLRNode->_bf = 0;
			parent->_bf = 1;
		}
		else
		{
			assert(false);
		}
	}
	void RotateRL(Node* parent)
	{
		Node* SubRNode = parent->_right;
		Node* SubRLNode = SubRNode->_left;
		int bf = SubRLNode->_bf;

		RotateR(SubRNode);
		RotateL(parent);

		if (bf == 0)
		{
			SubRNode->_bf = 0;
			SubRLNode->_bf = 0;
			parent->_bf = 0;
		}
		else if (bf == 1)
		{
			SubRNode->_bf = 0;
			SubRLNode->_bf = 0;
			parent->_bf = -1;
		}
		else if (bf == -1)
		{
			SubRNode->_bf = 1;
			SubRLNode->_bf = 0;
			parent->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	void Print()
	{
		_Print(_root);
		cout << endl;
	}
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key < cur->_kv.first)
			{
				cur = cur->_left;
			}
			else if (key > cur->_kv.first)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	bool IsBalanceTree()
	{
		return _IsBalanceTree(_root);
	}

private:
	void _Print(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_Print(root->_left);
		cout << root->_kv.first << " " << root->_kv.second<<endl;
		_Print(root->_right);
	}
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	bool _IsBalanceTree(Node* root)
	{
		if (nullptr == root)
			return true;

		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;

		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		}
		if (root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子异常" << endl;
			return false;
		}

		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}


	Node* _root = nullptr;
};